package com.yztstudy.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yztstudy.Mapper.CarouselMapper;
import com.yztstudy.Service.CarouselService;
import com.yztstudy.entity.Carousel;
import com.yztstudy.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class CarouselServiceImpl implements CarouselService {

    @Autowired
    private CarouselMapper carouselMapper;


    /**
     * TODO 查询优先级最高的六条记录
     * @return
     */
    @Cacheable(value = "list.carousel", key = "#root.methodName", cacheManager = "cacheManagerDay")
    @Override
    public R list() {

        // TODO 1.先按照优先级进行降序排序，获取所有的结果
        QueryWrapper<Carousel> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("priority");
        List<Carousel> carousels = carouselMapper.selectList(wrapper);

        // TODO 2.切割前6个
        List<Carousel> collect = carousels.stream().limit(6).collect(Collectors.toList());
        log.info("CarouselServiceImpl.list业务结束, 结果:{}", "查找前6个成功!");
        return R.ok("查找前6个成功!", collect);
    }
}
